package ntut.andengine.gamedesign;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTexture;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import android.app.Activity;
import android.content.Context;

public class GameSceneInit extends GameScene {

	// ===========================================================
	// Constants
	// ===========================================================
	
	// ===========================================================
	// Fields
	// ===========================================================

	private TextureRegion mGameTitleTextureRegion;
	private TextureRegion mStartButtonTextureRegion;
	
	// ===========================================================
	// Constructors
	// ===========================================================

	public GameSceneInit(Context context, Engine engine) {
		super(context, engine);
	}	
	
	// ===========================================================
	// Getter & Setter
	// ===========================================================

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	@Override
	public Engine onLoadEngine() {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public void onLoadResources() {		
		this.mBitmapTexture = new BitmapTexture(512, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		
		this.mGameTitleTextureRegion = BitmapTextureRegionFactory.createFromAsset(this.mBitmapTexture, mContext, "game_title.png", 0, 0);
		this.mStartButtonTextureRegion = BitmapTextureRegionFactory.createFromAsset(this.mBitmapTexture, mContext, "start_button.png", 0, 56);
		
		mEngine.getTextureManager().loadTexture(this.mBitmapTexture);	
	}

	@Override
	public Scene onLoadScene() {
		final int centerX = (CAMERA_WIDTH - this.mStartButtonTextureRegion.getWidth()) / 2;
		final int centerY = (CAMERA_HEIGHT - this.mStartButtonTextureRegion.getHeight()) / 2;
		
		final Sprite gameTitle = new Sprite(240 - this.mGameTitleTextureRegion.getWidth()/2, 100 - this.mGameTitleTextureRegion.getHeight()/2, this.mGameTitleTextureRegion);
		final Sprite starButton = new Sprite(centerX, centerY, this.mStartButtonTextureRegion){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.isActionDown()){
					MainGameActivity.startActivity((Activity) mContext, GameSceneLevel1.class);
					//changeScene(mBitmapTexture, new GameSceneLevel1(mContext, mEngine));
				}
				return true;
			}			
		};
		
		mScene.attachChild(gameTitle);
		mScene.attachChild(starButton);
		mScene.registerTouchArea(starButton);
		
		/* UPDATE */
		mScene.registerUpdateHandler(new IUpdateHandler(){
			float time;
			
			@Override
			public void onUpdate(float pSecondsElapsed) {
				time += pSecondsElapsed;
				
				
			}

			@Override
			public void reset() {
				// TODO Auto-generated method stub
				
			}
			
		});
		
		return mScene;
	}

	@Override
	public void onLoadComplete() {
		// TODO Auto-generated method stub
		
	}	
	
	// ===========================================================
	// Methods
	// ===========================================================

	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================

}
